--[[
FilePath: \Lua\View\UICost\UIGiftLink.lua
内测充值或等级达到返利
--]]

require 'View/UIBase/UIBase'

UIGiftLink = UIBase.New(PanelResNames.UIGiftLink)
local _M = UIGiftLink

_M.hasInit = false
_M.buyType = 1;
_M.heroId = -1;
_M.shopItem = {};
_M.titleItems = {};
_M.selectedItems = nil;
_M.buyItems = {};
_M.buyItemActId = {};
_M.listItems = nil;
_M.actState = -1;
_M.itemNum = 0;
_M.hasTimeEnd = false;
_M.baFunc = nil;
_M.hasBuyItem = false;
_M.isSkip = false;
_M.sevenRankPlusCost = nil;

function _M.Init(type, heroId, backFunc, isSkip, sevenRankPlusCost)
    _M.buyType = type or 1;
    if heroId == nil then
        _M.heroId = -1;
    else
        _M.heroId = tonumber(heroId);
    end
    _M.baFunc = backFunc;
    _M.isSkip = isSkip or false;
    _M.sevenRankPlusCost = sevenRankPlusCost;
    UIBase.Init(_M);
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("btnBack"), self.onClose);
        self.title = self:GetChild("title");
        self.list = self:GetChild("List");
        --self.list:RemoveChildrenToPool();
        self.bgPic = self:GetChild("BGPic");
        self.bgSign2Tips = self:GetChild("BgSign2Tips");
        self.bannerText = self:GetChild("bannerText");
        self.signTips1 = self:GetChild("Sign1Tips");
        self.signTips2 = self:GetChild("Sign2Tips");
        self.txtNum = self:GetChild("number");
        self.isFirstCtrl = self:GetController("IsFirst");
        self.isNullCtrl = self:GetController("IsNull");

        self.payCfg = G.dataTable["t_pay"];
        self.storeList = G.dataTable["t_storelist"];
        self.storeEntrance = G.dataTable["t_storeentrance"];
        _M.hasInit = true;

        self:addEvent()
    end
end

function _M:addEvent()
    self:RegisterEvent(Config.EventType.UICostUpdate,self.refreshItem);
end

function _M:removeEvent()
    self:UnRegisterEvent(Config.EventType.UICostUpdate, self.refreshItem);
end

function _M:refreshItem(type)
    if type == 0 or type == 2 or type == 1 then
        self:refresh();
        self.hasBuyItem = true;
    end
end

-- 显示
function _M:OnEnable()
    if not self.hasInit then
        self:OnBack();
        return;
    end
    logicMgr.NoticeManager.setMySevenRankPlusShopRedDots()
    FireEvent(Config.EventType.Fresh_SevenRankRedDot);
    self:refresh();
end

function _M:refresh()
    if self.sevenRankPlusCost ~= nil and self.sevenRankPlusCost.costId ~= nil then
        -- 七日排行榜集市特用的，fk
        self:updateView(self.sevenRankPlusCost.costId, self.sevenRankPlusCost.serverRankType, self.sevenRankPlusCost.timeCount);
        return;
    end

    if not self.isSkip then
        if self.actState ~= 1 then
            self.actState = logicMgr.CostManager.getFastConsumption();
        end
        if self.actState == -1 then
            self:OnBack();
            return;
        end
    end

    if self.listItems == nil then
        self:disposeItems();
    end

    if self.actState == 1 or self.isSkip then
        self.isFirstCtrl.selectedIndex = 0;
        self:selectedItem();
        if #self.buyItemActId < 1 then
            self.isNullCtrl.selectedIndex = 0;
            self.title.text = getLanguage("StoreTitle102150"..(self.buyType + 1));
            return;
        end
        self.isNullCtrl.selectedIndex = 1;
        if self.listItems == nil then
            self.title.text = getLanguage("StoreTitle102150"..(self.buyType + 1));
            self.listItems = {};
            local titleIndex = 0;
            local shopItemIndex = 0;
            for i = 1, #self.buyItemActId do
                local actId = self.buyItemActId[i];
                local buyItem = self.buyItems[tostring(actId)];
                if buyItem ~= nil then
                    table.sort(buyItem, self.sortItem);
                    local curListItem = {}
                    titleIndex = titleIndex + 1;
                    local item = nil;
                    local titleItem = nil;
                    if titleIndex <= #self.titleItems then
                        titleItem = self.titleItems[titleIndex];
                    else
                        titleItem = UIPackage.CreateObject("UIGiftLink", "GiftLinkList");
                        self.list:AddChild(titleItem)
                        table.insert(self.titleItems, titleItem);
                    end
                    titleItem.visible = true;
                    titleItem:GetChild("Tips").text = "";
                    local storeentrance = self.storeEntrance[tostring(actId)];
                    if storeentrance ~= nil then
                        local modelTableData = G.dataTable[storeentrance.f_ModelName][tostring(actId)];
                        if modelTableData ~= nil and modelTableData.f_Title ~= nil then
                            titleItem:GetChild("Tips").text = getLanguage(modelTableData.f_Title);
                        end
                    end
                    curListItem.titleItem = titleItem;
                    curListItem.shopItem = {};
                    for j = 1, #buyItem do
                        shopItemIndex = shopItemIndex + 1;
                        if shopItemIndex <= #self.shopItem then
                            item = self.shopItem[shopItemIndex];
                        else
                            item = UIPackage.CreateObject("UIGiftLink", "ButtonBuy");
                            self.list:AddChild(item)
                            table.insert(self.shopItem, item);
                        end
                        item.visible = true;
                        self:updateItem(item:GetChild("ButtonBuy"), buyItem[j].serverData, buyItem[j].tableData);
                        table.insert(curListItem.shopItem, item);
                    end
                    self.listItems[tostring(actId)] = curListItem;
                end
            end
        else
            for k, v in pairs(self.listItems) do
                local buyItem = self.buyItems[tostring(k)];
                if buyItem ~= nil then
                    table.sort(buyItem, self.sortItem);
                    for i = 1, #v.shopItem do
                        local item = v.shopItem[i];
                        if buyItem[i] ~= nil then
                            item.visible = true;
                            self:updateItem(item:GetChild("ButtonBuy"), buyItem[i].serverData, buyItem[i].tableData);
                        else
                            item.visible = false;
                        end
                    end
                else
                    v.titleItem.visible = false;
                    for i = 1, #v.shopItem do
                        local item = v.shopItem[i];
                        item.visible = false;
                    end
                end
            end
        end
    else
        logicMgr.CostManager.updateActiveOpenStatus(logicMgr.CostManager._FASTCONSUMPTION);
        self.isNullCtrl.selectedIndex = 1;
        self.serveritems, self.modelTableData, self.serverData = logicMgr.CostManager.getCostData(logicMgr.CostManager._FASTCONSUMPTION);
        self.signTips1.text = getLanguage(self.modelTableData.f_Tips1);
        self.signTips2.text = getLanguage(self.modelTableData.f_Tips2);
        self.title.text = getLanguage(self.modelTableData.f_Title);
        self.bgPic.url = self.modelTableData.f_Bg or "";
        self.bgSign2Tips.url = self.modelTableData.f_TitleBg or "";
        self.txtNum.text = self.modelTableData.f_number;
        self.isFirstCtrl.selectedIndex = 1;
        if self.serveritems ~= nil then
            local serverItems = self.serveritems.items;
            table.sort(serverItems, function(a, b)
                return a.f_ItemId < b.f_ItemId;
            end)
            for i = 1, #serverItems do
                local item = nil;
                local titleItem = nil;
                if i <= #self.titleItems then
                    titleItem = self.titleItems[i];
                else
                    titleItem = UIPackage.CreateObject("UIGiftLink", "GiftLinkList");
                    self.list:AddChild(titleItem)
                    table.insert(self.titleItems, titleItem);
                end
                titleItem.visible = true;
                titleItem:GetChild("Tips").text = getLanguage(serverItems[i].f_ProductName);

                if i <= #self.shopItem then
                    item = self.shopItem[i];
                else
                    item = UIPackage.CreateObject("UIGiftLink", "ButtonBuy");
                    self.list:AddChild(item)
                    table.insert(self.shopItem, item);
                end
                item.visible = true;
                --local tableData, localServerData = self:getTableData(logicMgr.CostManager._FASTCONSUMPTION, serverItems[i].f_ItemId, self.serverData);
                local tableData, localServerData = logicMgr.CostManager.getTableData(logicMgr.CostManager._FASTCONSUMPTION, serverItems[i].f_ItemId, self.serverData);
                self:updateItem(item:GetChild("ButtonBuy"), localServerData, tableData);
            end

            self.countDownNum = self.serveritems.endTime - HelperFunc.GetServerTime() + 5; -- 防止时间差
            self:TimeCall(self.OnCountDown, 1000, -1);
            self:OnCountDown();
        end
    end
end

function _M:updateView(costId, serverRankType, timeCount)
    self.serveritems, self.modelTableData, self.serverData = logicMgr.CostManager.getCostData(costId);
    if self.serverData == nil or self.modelTableData == nil or self.serveritems == nil then
        self.isNullCtrl.selectedIndex = 0;
        return;
    end
    logicMgr.CostManager.updateActiveOpenStatus(costId);
    self.isNullCtrl.selectedIndex = 1;
    self.signTips1.text = getLanguage(self.modelTableData.f_Tips1);
    self.signTips2.text = getLanguage(self.modelTableData.f_Tips2);
    self.title.text = getLanguage(self.modelTableData.f_Title);
    self.bgPic.url = self.modelTableData.f_Bg or "";
    self.bgSign2Tips.url = self.modelTableData.f_TitleBg or "";
    self.txtNum.text = self.modelTableData.f_number;
    self.isFirstCtrl.selectedIndex = 1;
    if self.serveritems ~= nil then
        local serverItems = self.serveritems.items;
        local curItems = {};
        for k, v in pairs(serverItems) do
            if serverRankType == nil or v.typeId == nil or tonumber(v.typeId) == tonumber(serverRankType) then
                table.insert(curItems, v);
            end
        end
        table.sort(curItems, function(a, b)
            return a.f_ItemId < b.f_ItemId;
        end)
        for i = 1, #curItems do
            local item = nil;
            local titleItem = nil;
            if i <= #self.titleItems then
                titleItem = self.titleItems[i];
            else
                titleItem = UIPackage.CreateObject("UIGiftLink", "GiftLinkList");
                self.list:AddChild(titleItem)
                table.insert(self.titleItems, titleItem);
            end
            titleItem.visible = true;
            titleItem:GetChild("Tips").text = getLanguage(curItems[i].f_ProductName);

            if i <= #self.shopItem then
                item = self.shopItem[i];
            else
                item = UIPackage.CreateObject("UIGiftLink", "ButtonBuy");
                self.list:AddChild(item)
                table.insert(self.shopItem, item);
            end
            item.visible = true;
            local tableData, localServerData = logicMgr.CostManager.getTableData(costId, curItems[i].f_ItemId, self.serverData);
            self:updateItem(item:GetChild("ButtonBuy"), localServerData, tableData);
        end

        if timeCount ~= nil then
            self.countDownNum = timeCount - HelperFunc.GetServerTime();
            self:TimeCall(self.sevenRankTimeCount, 1000, -1);
            self:sevenRankTimeCount();
        end
    end
end

function _M:sevenRankTimeCount()
    self.countDownNum = self.countDownNum - 1;
    if self.countDownNum <= 0 then
        self.bannerText.text = getLanguage("Sys_ActivityEnd");
        self:RemoveTimeCall(self.OnCountDown);
        self:onClose();
        return;
    end
    self.bannerText.text = string.format(getLanguage("Sys_ActivityTimeLeft"), HelperFunc.GetTimeStr(self.countDownNum));
end

function _M:OnCountDown()
    self.countDownNum = self.countDownNum - 1;
    if self.countDownNum <= 0 then
        self.bannerText.text = getLanguage("Sys_ActivityEnd");
        self:RemoveTimeCall(self.OnCountDown);
        if not self.hasTimeEnd then
            self.hasTimeEnd = true;
            self:refresh();
        end
        return;
    end
    self.bannerText.text = string.format(getLanguage("Sys_ActivityTimeLeft"), HelperFunc.GetSurpTimeStr(self.countDownNum));
end

function _M.sortItem(a, b)
    if (a.serverData.residue <= 0 and b.serverData.residue <= 0) or (a.serverData.residue > 0 and b.serverData.residue > 0) then
        return tonumber(a.tableData.f_Price) < tonumber(b.tableData.f_Price);
    else
        return a.serverData.residue > 0;
    end
end

function _M:selectedItem()
    if self.selectedItems == nil then
        self.selectedItems = {};
        for k, v in pairs(self.payCfg) do
            local quickBuy = v["f_QuickBuy" .. self.buyType];
            if quickBuy ~= nil and tonumber(quickBuy) > 0 and (v.f_BeforeConsumptionID == nil or tonumber(v.f_BeforeConsumptionID) == 0) then
                if self.buyType == 4 then
                    if v.f_HeroId == nil or tonumber(v.f_HeroId) == 0 or tonumber(v.f_HeroId) == self.heroId then
                        table.insert(self.selectedItems, v);
                    end
                else
                    table.insert(self.selectedItems, v);
                end
            end
        end
        table.sort(self.selectedItems, function(a, b)
            return tonumber(a.f_FromActivityId) < tonumber(b.f_FromActivityId);
        end);
    end
    local serverDataTab = {};
    local canBuyActId = {};
    self.buyItems = {};
    self.buyItemActId = {};
    local count = 0;
    for i = 1, #self.selectedItems do
        local item = self.selectedItems[i];
        local fromActivityId = tostring(item.f_FromActivityId);
        if serverDataTab[fromActivityId] == nil then
            local serverData = logicMgr.CostManager.getActiveServerDataList(item.f_FromActivityId);
            local serverTime = HelperFunc.GetServerTime();
            local localServerData = {};
            if serverData == nil or serverTime < serverData.startTime or (serverData.drawEndTime > 0 and serverTime > serverData.drawEndTime) then
                localServerData.serverData = nil;
            else
                localServerData.serverData = serverData;
            end
            serverDataTab[fromActivityId] = localServerData;
        end
        local serverDataItem = serverDataTab[fromActivityId].serverData;
        --local tableData, localServerData = self:getTableData(fromActivityId, item.f_ItemId, serverDataItem);
        local tableData, localServerData = logicMgr.CostManager.getTableData(fromActivityId, item.f_ItemId, serverDataItem);
        if tableData ~= nil and localServerData.residue ~= nil then
            if tableData.f_Num == -1 or localServerData.countSum == nil or tonumber(localServerData.countSum) > 0 or (localServerData.residue <= 0 and tonumber(localServerData.countSum) <= 0) then
                local localBuyItem = {};
                if localServerData.residue > 0 or item.f_NextConsumptionID == nil or tonumber(item.f_NextConsumptionID) == 0 then
                    localBuyItem.tableData = tableData;
                    localBuyItem.serverData = localServerData;
                else
                    --local backTableData, backLocalServerData = self:getNextConsumptionData(tableData, localServerData, item, serverDataItem);
                    local backTableData, backLocalServerData = logicMgr.CostManager.getNextConsumptionData(tableData, localServerData, item, serverDataItem, self.buyType);
                    localBuyItem.tableData = backTableData;
                    localBuyItem.serverData = backLocalServerData;
                end
                if self.buyItems[fromActivityId] == nil then
                    self.buyItems[fromActivityId] = {};
                    table.insert(self.buyItemActId, tonumber(fromActivityId));
                    canBuyActId[fromActivityId] = 0;
                end
                if localBuyItem.serverData.residue > 0 then
                    canBuyActId[fromActivityId] = 1;
                end
                count = count + 1;
                table.insert(self.buyItems[fromActivityId], localBuyItem);
            end
        end
    end
    if self.itemNum ~= count then
        self.itemNum = count;
        self.listItems = nil;
        self:disposeItems();
    end
    table.sort(self.buyItemActId, function(a, b)
        if canBuyActId[tostring(a)] == canBuyActId[tostring(b)] then
            return a < b;
        end
        return canBuyActId[tostring(a)] > canBuyActId[tostring(b)];
    end)
end

function _M:disposeItems()
    if self.titleItems ~= nil then
        for i = 1, #self.titleItems do
            self.titleItems[i]:Dispose();
        end
    end
    self.titleItems = {};
    if self.shopItem ~= nil then
        for i = 1, #self.shopItem do
            self.shopItem[i]:Dispose();
        end
    end
    self.shopItem = {};
end

--function _M:getNextConsumptionData(backTableData, backServerData, cfg, serverData)
--    local itemCfg = self.payCfg[tostring(cfg.f_NextConsumptionID)];
--    if itemCfg == nil or itemCfg.f_FromActivityId ~= cfg.f_FromActivityId then
--        return backTableData, backServerData;
--    end
--    --local tableData, localServerData = self:getTableData(itemCfg.f_FromActivityId, itemCfg.f_ItemId, serverData);
--    local tableData, localServerData = logicMgr.CostManager.getTableData(itemCfg.f_FromActivityId, itemCfg.f_ItemId, serverData);
--    if tableData == nil or localServerData.residue == nil then
--        return backTableData, backServerData;
--    end
--    local quickBuy = itemCfg["f_QuickBuy"..self.buyType];
--    if localServerData.residue > 0 then
--        if quickBuy ~= nil and tonumber(quickBuy) > 0 then
--            if localServerData.countSum == nil or localServerData.countSum > 0 then
--                return tableData, localServerData;
--            else
--                return backTableData, backServerData;
--            end
--        else
--            return backTableData, backServerData;
--        end
--    else
--        if itemCfg.f_NextConsumptionID == nil or tonumber(itemCfg.f_NextConsumptionID) == 0 then
--            if quickBuy ~= nil and tonumber(quickBuy) > 0 then
--                return tableData, localServerData;
--            else
--                return backTableData, backServerData;
--            end
--        else
--            return self:getNextConsumptionData(tableData, localServerData, itemCfg, serverData);
--        end
--    end
--end

function _M:updateItem(itemObj, serverData, tableData)
    if tableData == nil then
        itemObj.visible = false;
        return;
    end
    local bg = itemObj:GetChild("bg");
    local mask = itemObj:GetChild("mask");
    local buy = itemObj:GetChild("numMoney");
    local ntext = itemObj:GetChild("ntext");
    local buyNum = itemObj:GetChild("numLimit");
    local fristItem = itemObj:GetChild("item");
    local char = itemObj:GetChild("char");
    local itemList = itemObj:GetChild("itemList");
    local original = itemObj:GetChild("original");
    local originalMoney = itemObj:GetChild("originalMoney");
    local stateController = itemObj:GetController("state");
    local typeController = itemObj:GetController("type");
    local discountController = itemObj:GetController("discount");
    local isfreeController = itemObj:GetController("isfree");
    local freeCtrl = itemObj:GetController("IsFree");
    local freeText = itemObj:GetChild("freetext");
    local itemMoney = itemObj:GetChild("ItemMoney");
    local residue = serverData.residue or 0;
    buyNum.text = residue.."/"..tableData.f_Count;
    if residue <= 0 then
        stateController.selectedIndex = 1;
    else
        stateController.selectedIndex = 0;
    end
    --bg.url = tostring(tableData.f_BgPic);
    --mask.url = tostring(tableData.f_BgPic);
    bg.onClick:Clear();
    local reward;
    if tableData.f_ActivityId ~= nil and tableData.f_ActivityId ~= "" and tableData.f_NewMajorCommodityReward ~= nil and tableData.f_NewMajorCommodityReward ~= "" then
        local activityId = tableData.f_ActivityId
        local newReward = decodeJsonStrToTabel(tableData.f_NewMajorCommodityReward);
        local newRewardNum = #newReward;
        for i = 1, #activityId do
            local isEnd = logicMgr.ActiveManager.IsActiveEnd(activityId[i]);
            if not isEnd and i <= newRewardNum then
                reward = decodeJsonStrToTabel(tableData.f_NewMajorCommodityReward)[i][1];
                break;
            end
        end
    end
    if reward == nil then
        reward = decodeJsonStrToTabel(tableData.f_MajorCommodityReward)[1];
    end
    if tableData.f_Type == 1 then
        typeController.selectedIndex = 1;
        char.url = tostring(tableData.f_BannerHeroHead);
        bg.onClick:Add(function()
            showUI(PanelNames.UIActivityPublicConfirm, nil, self.activeId, tableData.f_ItemId);
        end);
        logicMgr.CostManager.RefreshItem(fristItem, {reward[1] + 201500});
    else
        typeController.selectedIndex = 0;
        --if tableData.f_Type == 2 then
        --    typeController.selectedIndex = 2;
        --end
        bg.onClick:Add(function()
            if tableData.f_PriceType ~= nil and tonumber(tableData.f_PriceType) == 1 then
                -- 钻石购买
                local curNum = logicMgr.ItemManager.getCurrencyCountByModelId(tonumber(1));
                if curNum < tonumber(tableData.f_Price) then
                    UISysTips.AddMsg(string.format(getLanguage("Character_Tips19"),ItemManager.GetItemName(CurrencyEnum.GOLD)));--红魔石不足
                else
                    local data = {};
                    table.insert(data, tableData.f_ItemId);
                    logicMgr.CostManager.getRewardByCurrency(tableData.f_BelongActiveId, data, nil, self);
                end
            else
                local storeType = tonumber(tableData.f_StoreType);
                if storeType == 3 or storeType == 13 or storeType == 15 then
                    if serverData.countSum ~= nil and serverData.countSum == -10 then
                        local data = {};
                        table.insert(data, tableData.f_ItemId);
                        logicMgr.CostManager.getRewardById(tableData.f_BelongActiveId, data, nil, self);
                    else
                        logicMgr.CostManager.getRewardByIdAndIndex(tableData.f_BelongActiveId, tableData.f_ItemId, tableData.f_ProductId, 1, tableData.f_Price, nil, self);
                    end
                end
            end

        end);
        logicMgr.CostManager.RefreshItem(fristItem, reward);
    end
    if serverData.countSum == nil or serverData.countSum < 0 then
        freeCtrl.selectedIndex = 0;
    else
        freeCtrl.selectedIndex = 1;
        freeText.text = string.format(getLanguage("storedesc1020201"), serverData.countSum);
    end
    if tableData.f_OriginPrice == nil or tableData.f_OriginPrice == 0 then
        discountController.selectedIndex = 0;
    else
        discountController.selectedIndex = 1;
        originalMoney.text = tableData.f_OriginPrice;
    end
    ntext.text = getLanguage("SellLimit");
    if tableData.f_Price == 0 then
        isfreeController.selectedIndex = 1;
        buy.text = getLanguage("FreeMoney");
    elseif tableData.f_PriceType ~= nil and tonumber(tableData.f_PriceType) == 1 then
        itemMoney.text = tostring(tableData.f_Price);
        typeController.selectedIndex = 3;
    else
        if serverData.countSum ~= nil and serverData.countSum == -10 then
            isfreeController.selectedIndex = 1;
            buy.text = getLanguage("FreeMoney");
        else
            isfreeController.selectedIndex = 0;
            buy.text = "￥"..tableData.f_Price;
        end
    end
    local listData = decodeJsonStrToTabel(tableData.f_CommonCommodityReward);
    logicMgr.CostManager.RefreshItemList(itemList, listData);
end

--function _M:getTableData(storeId, itemId, serverData)
--    if storeId == nil then
--        return nil;
--    end
--    local modelTableData = nil;
--    local tableDataItem = nil;
--
--    local activeTable = self.storeList[tostring(storeId)] or {};
--    local storeentrance = self.storeEntrance[tostring(storeId)];
--    if storeentrance ~= nil then
--        modelTableData = G.dataTable[storeentrance.f_ModelName][tostring(storeId)];
--        if modelTableData == nil then
--            return nil;
--        end
--    else
--        return nil;
--    end
--
--    local type = tonumber(activeTable.f_StoreType);
--    local tableData = {};
--    local localServerData = {};
--    localServerData.residue = nil;
--    localServerData.countSum = nil;
--    if type == 1 then
--        --local tableDataCfg = G.dataTable[modelTableData.f_ItemTableName];
--        --if tableDataCfg ~= nil then
--        --    tableDataItem = tableDataCfg[tostring(itemId)];
--        --else
--        --    return nil;
--        --end
--        --tableData = self:getTableDataModel();
--        --tableData.f_ItemId = tableDataItem.f_MonthlyCardId;
--        --tableData.f_BelongActiveId = tableDataItem.f_BelongActiveId;
--        --tableData.f_Price = tableDataItem.f_Price;
--        --tableData.f_Count = tableDataItem.f_RepeatBuy;
--        --tableData.f_ProductId = tableDataItem.f_ProductId;
--        --tableData.f_MajorCommodityReward = tableDataItem.f_MajorCommodityReward;
--        --tableData.f_CommonCommodityReward = tableDataItem.f_CommonCommodityReward;
--        --tableData.f_StoreType = type;
--        --return tableData;
--        return nil;
--    elseif type == 2 then
--        return nil;
--    elseif type == 3 then
--        local tableDataCfg = G.dataTable[modelTableData.f_ItemTableName];
--        if tableDataCfg ~= nil then
--            tableDataItem = tableDataCfg[tostring(itemId)];
--        else
--            return nil;
--        end
--        if tableDataItem == nil then
--            return nil;
--        end
--        if not logicMgr.CostManager.isOpenItem(tableDataItem) then
--            return nil;
--        end
--
--        tableData = tableDataItem;
--        tableData.f_StoreType = type;
--
--        if serverData ~= nil then
--            local buyCountMap = {};
--            local buyCountSumMap = {};
--            if serverData.marketData ~= nil then
--                if serverData.marketData.buyCountMap ~= nil then
--                    buyCountMap = serverData.marketData.buyCountMap;
--                end
--                if serverData.marketData.buyCountSumMap ~= nil then
--                    buyCountSumMap = serverData.marketData.buyCountSumMap;
--                end
--            end
--            if tableData.f_Num ~= -1 then
--                local countSum = buyCountSumMap[tostring(tableData.f_ItemId)] or 0;
--                local cfgNum = tableData.f_Num;
--                if countSum < cfgNum then
--                    localServerData.countSum = cfgNum - countSum;
--                else
--                    localServerData.countSum = -10;
--                end
--            end
--            local buyCount = buyCountMap[tostring(tableData.f_ItemId)] or 0;
--            localServerData.residue = tableData.f_Count - buyCount;
--        end
--
--        return tableData, localServerData;
--    elseif type == 4 then
--        return nil;
--    elseif type == 5 then
--        return nil;
--    elseif type == 6 then
--        return nil;
--    elseif type == 7 then
--        return nil;
--    elseif type == 8 then
--        return nil;
--    elseif type == 9 then
--        return nil;
--    elseif type == 10 then
--        return nil;
--    elseif type == 11 then
--        return nil;
--    elseif type == 12 then
--        return nil;
--    elseif type == 13 then
--        local tableDataCfg = G.dataTable[modelTableData.f_ItemTableName];
--        if tableDataCfg ~= nil then
--            tableDataItem = tableDataCfg[tostring(itemId)];
--        else
--            return nil;
--        end
--        if tableDataItem == nil then
--            return nil;
--        end
--        tableData = self:getTableDataModel();
--        tableData.f_ItemId = tableDataItem.f_ItemId;
--        tableData.f_BelongActiveId = tableDataItem.f_StoreId;
--        tableData.f_Price = tableDataItem.f_Price;
--        tableData.f_OriginPrice = tableDataItem.f_OriginPrice;
--        tableData.f_Count = tableDataItem.f_Count;
--        tableData.f_ProductId = tableDataItem.f_ProductId;
--        tableData.f_MajorCommodityReward = tableDataItem.f_MajorCommodityReward;
--        tableData.f_CommonCommodityReward = tableDataItem.f_CommonCommodityReward;
--        tableData.f_StoreType = type;
--
--        if serverData ~= nil then
--            local buyMarketSet = {};
--            if serverData.marketData ~= nil then
--                local serverBuyMarketSet = serverData.marketData.buyMarketSet;
--                if serverBuyMarketSet ~= nil then
--                    for k, v in pairs(serverBuyMarketSet) do
--                        buyMarketSet[tostring(v)] = 1;
--                    end
--                end
--            end
--            local buyCount = buyMarketSet[tostring(tableData.f_ItemId)] or 0;
--            localServerData.residue = tableData.f_Count - buyCount;
--        end
--        return tableData, localServerData;
--    else
--        return nil;
--    end
--end
--
--function _M:getTableDataModel()
--    local tableData = {};
--    tableData.f_ItemId = 0;
--    tableData.f_BelongActiveId = 0;
--    tableData.f_Price = 0;
--    tableData.f_Count = 1;
--    tableData.f_Num = -1;
--    tableData.f_MajorCommodityReward = "";
--    tableData.f_CommonCommodityReward = "";
--    tableData.f_ActivityId = "";
--    tableData.f_NewMajorCommodityReward = "";
--    tableData.f_BgPic = "";
--    tableData.f_title = "";
--    tableData.f_NextConsumptionID = 0;
--    tableData.f_BeforeConsumptionID = 0;
--    tableData.f_Order = 0;
--    tableData.f_AndRelation = "";
--    tableData.f_OrRelation = "";
--    tableData.f_NeedRencencyLevel = 0;
--    tableData.f_UnlockStage = 0;
--    tableData.f_UnlockTime = 0;
--    tableData.f_UnlockVipRank = 0;
--    tableData.f_ProductId = 0;
--    tableData.f_Type = 0;
--    tableData.f_BannerHeroHead = "";
--    tableData.f_OriginPrice = 0;
--    tableData.f_Discount = 0;
--    return tableData;
--end

function _M:onClose()
    if self.baFunc ~= nil then
        if type(self.baFunc) == "function" and self.hasBuyItem then
            self.baFunc();
        end
    else
        if self.hasBuyItem then
            FireEvent(Config.EventType.FastConsumption, self.buyType);
        end
    end

    self:OnBack();
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    self:disposeItems();
    self:RemoveTimeCall(self.OnCountDown);
    self.shopItem = {};
    self.titleItems = {};
    self.selectedItems = nil;
    self.buyItems = {};
    self.listItems = nil;
    self.buyItemActId = {};
    self.actState = -1;
    self.hasTimeEnd = false;
    self.baFunc = nil;
    self.hasBuyItem = false;
    self.hasInit = false;
    self.isSkip = false;
    self.sevenRankPlusCost = nil;
end

return _M;